
//
// 110527 - AH - (Garage) Created.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing.rethor;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import org.soekrat.engine.forest.Forest;
import org.soekrat.engine.ontology.OntologyDatastructure;
import org.soekrat.engine.ontology.Sentence;
import org.soekrat.engine.ontology.SentenceBasedOntology;
import org.soekrat.gui.swing.TestFrame;

/**
 * <p>
 *   A view that shows sentences of an
 *   {@link SentenceBasedOntology} 
 *   {@link OntologyDatastructure}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class SentenceBasedOntologyView extends JPanel 
{
  private final static class TM extends AbstractTableModel
  {
    //private final SentenceBasedOntology m_ontology;
    
    private Sentence[] m_sentences=Sentence.EMPTY_ARRAY;
    
    TM(){ /*m_ontology=null;*/ }
    
    TM(SentenceBasedOntology ontology)
    {
      if (ontology==null)
      {
        throw new NullPointerException("ontology");
      }
      
      //m_ontology=ontology;
      m_sentences=ontology.getSentences();
      
      //System.out.println("Sentences: "+Arrays.toString(m_sentences));
    }

    // Specified in TableModel.
    public int getColumnCount(){ return 1; }

    // Specified in TableModel.
    public int getRowCount(){ return m_sentences.length; }

    // Specified in TableModel.
    public Object getValueAt(int row, int col)
    {
      return m_sentences[row];
    }

    @Override
    public String getColumnName(int column)
    {
      return "Sentences";
    }
    
    public Sentence getSentence(int idx){ return m_sentences[idx]; }
    
  }
  
  private final static TM EMPTY_MODEL=new TM();
  
  private JTable m_table=new JTable(EMPTY_MODEL);
  
  private SentenceBasedOntology m_ontology=null;

  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public SentenceBasedOntologyView()
  {
    setLayout(new BorderLayout());
    
    add(new JScrollPane(m_table));
  }
  
  /**
   * <p>
   *   Sets 
   *   {@link SentenceBasedOntology} to view.
   * </p>
   * @param ontology {@code SentenceBasedOntology} to view or {@code null} if 
   *                 to clear.
   */
  public void setOntology(SentenceBasedOntology ontology)
  {
    m_ontology=ontology;
    if (ontology==null) m_table.setModel(EMPTY_MODEL); 
    else                m_table.setModel(new TM(ontology));
  }
  
  /**
   * <p>
   *   Gets selected 
   *   {@link Sentence} in table.
   * </p>
   * @return {@link Sentence} or {@code null} if non selected.
   */
  public Sentence getSelected()
  {
    int row=m_table.getSelectedRow();
    if (row==-1) return null;
    TM model=(TM)m_table.getModel();
    return model.getSentence(row);
  }
  
  /**
   * <p>
   *   Delete 
   *   {@link Sentence} from viewed 
   *   {@link OntologyDatastructure}.
   * </p>
   * @param sentence {@link Sentence} to delete.
   */
  public void deleteSentence(Sentence sentence)
  {
    if (sentence==null)
    {
      throw new NullPointerException("sentence");
    }
    
    if (m_ontology!=null) 
    {
      //System.out.println("BEFORE DELETE");
      //System.out.println(m_ontology);
      //System.out.println("========");
      m_ontology.deleteSentence(sentence);
      //System.out.println("AFTER DELETE");
      //System.out.println(m_ontology);
      //System.out.println("========");
    }
    //refresh();
  }
  
  public static void main(String[] args) 
  {
    Forest forest=new Forest();
    forest.populate
    (
      new String[]
      {
        "Arne is writing code",
      }
    );
    
    forest.populate
    (
      new String[]
      {
        "Laetitia is dreaming code",
      }
    );
    
    SentenceBasedOntologyView view=new SentenceBasedOntologyView();
    view.setOntology(forest);
    TestFrame.show(view);
  }
  
}
